//
//  AppDetailController.m
//  gdemm
//
//  Created by 乔明超 on 2017/8/8.
//  Copyright © 2017年 Goldencis. All rights reserved.
//

#import "AppDetailController.h"
#import "HeaderLabel.h"
#import "Utils.h"
#import <objc/runtime.h>
#import "AppDetailTableViewCell.h"
#import "UIImageView+WebCache.h"
@interface AppDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic) NSMutableArray *mTitleSectionArr;
@property (weak, nonatomic) IBOutlet UITableView *mTableView;
@property(strong,nonatomic) NSMutableArray *mCellSectionArr;
@property (weak, nonatomic) IBOutlet UIImageView *iconImage;
@property (weak, nonatomic) IBOutlet UILabel *appNameLabel;
@property (weak, nonatomic) IBOutlet UILabel *sizeLabel;

@end

@implementation AppDetailController
-(NSMutableArray *)mCellSectionArr{
    if (_mCellSectionArr == nil) {
        _mCellSectionArr = [NSMutableArray array];
            [_mCellSectionArr addObject:_model.remark];
            [_mCellSectionArr addObject:_model.updateLog];
            [_mCellSectionArr addObject:_model.version];
            [_mCellSectionArr addObject:_model.updateTime];
    }
    return _mCellSectionArr;
}

- (NSMutableArray *)mTitleSectionArr{
    if (_mTitleSectionArr == nil) {
        _mTitleSectionArr = [NSMutableArray array];
            [_mTitleSectionArr addObject:@"应用描述"];
            [_mTitleSectionArr addObject:@"更新日志"];
            [_mTitleSectionArr addObject:@"版本"];
            [_mTitleSectionArr addObject:@"更新时间"];
    }
    return _mTitleSectionArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBtnState];
    [self.stateBtn setBackgroundImage:[UIImage imageNamed:@"state_state_click"] forState:UIControlStateSelected];
    self.stateBtn.layer.cornerRadius = 25;
    self.stateBtn.layer.masksToBounds = YES;
    [self.stateBtn.titleLabel setTextColor:[Utils UIColorFromHex:0xffffff]];
    
    //添加返回按钮
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.mTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.mTableView.showsVerticalScrollIndicator  = NO;
    if ([DeviceTypeJudge isPadDeviceType]) {
        leftButton.frame = CGRectMake(0, 0, 20, 20);
    }else{
        leftButton.frame = CGRectMake(0, 0, 16, 16);
    }
    
    [leftButton setImage:[[UIImage imageNamed:@"navigationBar_back"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
    [leftButton addTarget:self.navigationController action:@selector(popViewControllerAnimated:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftBarButton;
    self.navigationItem.title = @"应用详情";
    self.mTableView.tableHeaderView.height = 220;
    //加载应用信息
    self.appNameLabel.text = self.model.name;
    self.sizeLabel.text = self.model.size;
    NSString *servAddr = [[UserDefaultsUtil shareInstance]getString:LC_SERVADDR];
    if(![StringUtil isEmpty:servAddr]){
        NSString * iconPathStr = [NSString stringWithFormat:@"http://%@%@",servAddr,self.model.iconPath];
        NSURL * url = [NSURL URLWithString:iconPathStr];
        [self.iconImage sd_setImageWithURL:url];
    }
    self.iconImage.layer.cornerRadius = 20;
    self.iconImage.layer.masksToBounds = YES;
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.mCellSectionArr.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AppDetailTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"detail"];
    if (cell == nil) {
        cell = [[AppDetailTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"detail"];
    }
    NSString * titile = self.mCellSectionArr[indexPath.section];
    if (titile == nil || titile.length == 0) {
            cell.contentLabel.text = @"无" ;
    }else{
            cell.contentLabel.text = titile;
    }

    [cell.contentLabel setTextColor:[Utils UIColorFromHex:0x888888]];
    cell.contentLabel.font = [UIFont systemFontOfSize:15];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return self.mTitleSectionArr[section];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    HeaderLabel *label = [[HeaderLabel alloc]init];
    label.textLabel.text = self.mTitleSectionArr[section];
    return label;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString * content = self.mCellSectionArr[indexPath.section];
    CGFloat height = [content boundingRectWithSize:CGSizeMake(WINDOWWIDTH, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]} context:nil].size.height;
    return height + 15;
}
- (IBAction)stateBtn:(id)sender {
    if ([self.model.appState isEqualToString:@"打开"]) {
        Class LSApplicationWorkspace_class = objc_getClass("LSApplicationWorkspace");
        NSObject *workspace = [LSApplicationWorkspace_class performSelector:@selector(defaultWorkspace)];
        //通过Bundle打开app
        [workspace performSelector:NSSelectorFromString(@"openApplicationWithBundleID:") withObject:self.model.packageName];
    }else if([self.model.appState  isEqualToString:@"安装"] || [self.model.appState  isEqualToString:@"更新"]) {
        NSString *servAddr = [[UserDefaultsUtil shareInstance]getString:LC_SERVADDR];
        NSString * url = [NSString stringWithFormat:@"itms-services://?action=download-manifest&url=https://%@/Resource/%@",servAddr,self.model.plistPath];
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url]];
    }
}



-(void)setBtnState{
    //状态按钮的设置
    if ([self.model.appState isEqualToString:@"打开"]) {
        [self.stateBtn setBackgroundImage:[Utils imageWithKey:@"state_open"] forState:UIControlStateNormal];
        [self.stateBtn setTitle:@"打开" forState:UIControlStateNormal];
        [self.stateBtn setTitleColor:[Utils UIColorFromHex:0x26365a] forState:UIControlStateNormal];
    }else if ([self.model.appState isEqualToString:@"更新"]){
        [self.stateBtn setBackgroundImage:[UIImage imageNamed:@"state_update"] forState:UIControlStateNormal];
        [self.stateBtn setTitle:@"更新" forState:UIControlStateNormal];
    }else if([self.model.appState isEqualToString:@"安装"]){
        [self.stateBtn setBackgroundImage:[Utils imageWithKey:@"state_install"] forState:UIControlStateNormal];
        [self.stateBtn setTitle:@"安装" forState:UIControlStateNormal];
    }else{
        [self.stateBtn setBackgroundImage:[Utils imageWithKey:@"state_install"] forState:UIControlStateNormal];
        [self.stateBtn setTitle:self.model.appState forState:UIControlStateNormal];
    }

}
@end
